package com.example.jdbcdemo.controller;


import com.alibaba.fastjson.JSON;
import com.example.jdbcdemo.mapper.StorageMapper;
import com.example.jdbcdemo.pojo.Storage;
import com.example.jdbcdemo.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/storage")
public class StorageController {
    @Autowired
    StorageMapper storageMapper;

    @PostMapping("/list")
    public JsonUtil queryStorageList(){
        List<Storage> storageList=storageMapper.queryStorageList();
        if (storageList.isEmpty()){
            return new JsonUtil(400,"无仓库信息","[]");
        }
        return new JsonUtil(0,"查询成功", JSON.toJSONString(storageList));
    }

    @PostMapping("/add")
    public JsonUtil addStorage(@RequestBody Map<String,String> map){
        storageMapper.addStorage(new Storage(0,map.get("address")));//主键自增没用到id所以id随便
        return new JsonUtil(0,"添加成功","");
    }

    @PostMapping("/queryById")
    public JsonUtil queryById(@RequestBody Map<String,Integer> map){
        Storage storage= storageMapper.queryStorageById(map.get("id"));
        if (storage==null)
        {
            return new JsonUtil(400,"未找到","");
        }
        return new JsonUtil(0,"查询成功",JSON.toJSONString(storage));
    }

    @PostMapping("/deleteById")
    public JsonUtil deleteStorage(@RequestBody Map<String,Integer> map){
        storageMapper.deleteStorage(map.get("id"));
        return new JsonUtil(0,"删除成功","");
    }
}
